package Atop100.heap;

import java.util.PriorityQueue;

public class Class215 {

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {

            if (queue.size()<k){
                queue.offer(nums[i]);
            }
            else {
                if (nums[i]>queue.peek()){
                    queue.poll();
                    queue.add(nums[i]);
                }
            }

        }

//
//        for (int i = 0; i < queue.size(); i++) {
//            System.out.println(queue.poll());
//        }
        return  queue.peek();

    }

    public static void main(String[] args) {
        Class215 class215 = new Class215();
        int[] arr={3,2,3,1,2,4,5,5,6};
        System.out.println(class215.findKthLargest(arr, 4));

    }
}
